package com.foreknow.demo7;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * @author shkstart
 * @create 2021-07-27 15:33
 */
public class EmployeeDaoImpl extends Employee implements EmployeeDao {
    static int total = 0;
    Employee[] employees;
    List<Employee> employees1;

    public EmployeeDaoImpl() {
        employees = new Employee[20];
        employees1 = new ArrayList<>();
    }

    @Override
    public void add(Employee employee) {
        //1.判断参数employee是否为空，如果不为空
        if (employee != null) {
            //2.如果count<数组定义的长度 (20)
            if (total < 20) {
                //3.要将employee对象添加到数组容器中,count加1
                //employees[total++] = employee;
                employees1.add(employee);
                //4.输出信息
                System.out.println("添加成功");
            } else {
                throw new MyException("数组越界");
            }
        }
    }

    @Override
    public List<Employee> searchAll() {
        //1.要读employees数组进行遍历，每遍历一次要将数组中获取到的员工对象保存到新数组中
        Employee[] newemp = new Employee[employees.length];
        List<Employee> newemp1 = new ArrayList<>();
//        for (int i = 0; i < total; i++) {
//            System.out.println(newemp[i].getId()+"\t"+newemp[i].getName()+"\t"+newemp[i].getSal()+"\t"+newemp[i].getDept());
//        }
        newemp = employees;
        newemp1 = employees1;
        //2.返回新的数组
        return newemp1;
    }

    @Override
    public Employee upDate(Employee employee) {
        //1.可以先根据id 查询要修改的对象
        //2.然后将查询到的对象传递给参数进行修改(set方法)
        Employee upemp = search(employee.getId());
        upemp.setName("已修改");
        return upemp;

//        Employee e = new Employee("Q", "Q", 1, "Q");
//        employee = e;
//        return employee;

    }

    @Override
    public Employee search(String id) {
        //1.对employees数组进行遍历
        //2.每遍历一次就要获取到一个Employee对象,判断Employee对象的id是否与参数中的id是否相等
        //3.最后将查询的对象返回
        Employee e;
//        for (int i = 0; i < total; i++) {
//            if (id.equals(employees[i].getId())) {
//                e = employees[i];
//                return e;
//            }
//        }
        for (int i = 0; i < employees1.size(); i++) {
            if(id.equals(employees1.get(i).getId())){
                e=employees1.get(i);
                return e;
            }
        }
        return null;
    }

    public void showAll(List<Employee> arr) {
        for (int i = 0; i < arr.size(); i++) {
            System.out.println(arr.get(i).toString());
        }
    }

    public static void main(String[] args) {
        Employee e1 = new Employee("1", "tom", 5000, "mag");
        Employee e2 = new Employee("2", "jerry", 8000, "header");
        Employee e3 = new Employee("3", "jane", 3000, "cleaner");
        Employee e4 = new Employee("4", "dave", 5000, "teacher");
        EmployeeDaoImpl emp = new EmployeeDaoImpl();
        try {
            emp.add(e1);
            emp.add(e2);
            emp.add(e3);
            emp.add(e4);
        } catch (MyException e) {
            e.getMessage();
        }
//        Employee[] newes = emp.searchAll();
        List<Employee> newes=new ArrayList<>();
        System.out.println("修改前");
        newes = emp.searchAll();
        emp.showAll(newes);
        Employee employee = emp.upDate(e2);

//        for (int i = 0; i < total; i++) {
////            System.out.println(newes[i].getId()+"\t"+newes[i].getName()+"\t"+newes[i].getSal()+"\t"+newes[i].getDept());
//            System.out.println(newes[i].toString());
//        }
        System.out.println("修改后");
        emp.showAll(newes);
        System.out.println("请输入要找到id");
        Scanner sc = new Scanner(System.in);
        String findid = sc.next();
        Employee find = emp.search(findid);
        if (find != null) {
            System.out.println("找到了");
            System.out.println(find.toString());
        } else {
            System.out.println("没有找到");
        }

    }

}
